package com.fairytask.objects
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.managers.ResManager;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.utils.Vec2D;
	import com.greensock.TimelineMax;
	import com.greensock.TweenLite;
	import com.greensock.easing.Linear;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	import flash.media.Sound;
	
	public class Cell extends Sprite
	{
		//显示
		private var _cell	:MovieClip;
		private var _showUnits:Boolean=true;
		private var _maxUnits:uint=0;
		private var _scale:Number;
		private var _unitValue:Number=1;
		
		private var _isStart:Boolean=false;
		
		public var vel:Number=0;
		
		private var cellSnd:flash.media.Sound;
		
		private var _radius:Number=0;
		
		private var _totalTime:int=0;
		
		public var ov:Vec2D;
		
		private var _numUnits:Number=0;
		
		private var _txt:MovieClip;
		
		private var desTex	:MovieClip;
		
		private var _curTime:int=0;
		
		private var _lastTime:int=0;
		
		private var _unitType:int=-1;
		private var friction:Number=0.97;
		private var _ringShow	:Boolean	=	false;
		private var _selected	:Boolean	=	false;
		private var _color:uint;
		
		//设置缓动
		private var tweenMax	:TimelineMax;
		
		public function Cell(pSkin:String)
		{
			tweenMax	=	new TimelineMax({});
			
			mouseChildren	=	false;
			ov = new Vec2D();
			_cell = ResManager.newResClass(pSkin) as MovieClip;
			_cell.rotation = Math.random() * 360;
			_cell.cacheAsBitmap	=	true;
			//写文字
			desTex	=	ResManager.newResClass("cell_txt") as MovieClip;
			desTex.txt.mouseEnabled	=	false;
			desTex.mouseEnabled	=	false;
			desTex.mouseChildren	=	false;
			addChild(desTex);
			addChild(_cell);
			
			radius = 100;
			//注册事件
			initEvent();
		}
		
		/**
		 * 设置事件监听 
		 * 
		 */
		private function initEvent():void
		{
			//addEventListener(MouseEvent.ROLL_OVER,
		}
		
		public function playScaleEffect():void
		{
			tweenMax.append(TweenLite.to(_cell,0.2,{scaleX:_scale + 0.1, scaleY:_scale + 0.1,  ease:Linear.easeIn}));
			tweenMax.append(TweenLite.to(_cell,0.5,{scaleX:_scale, scaleY:_scale,  ease:Linear.easeIn}));
			tweenMax.play();
		}
		
		/**
		 * 设定大小 
		 * @param pScale
		 * 
		 */
		public function init(pScale:Number):void
		{
			_scale = pScale;
			_cell.scaleX = _cell.scaleY	=	pScale;
			desTex.scaleX = desTex.scaleY	=	pScale;
			
			radius = 30 * pScale;
			
			_maxUnits = _radius * 3;
			_totalTime = Math.abs(1 - _radius / 100) * 2500;
		}
		
		public function set numUnits(pNum:Number):void
		{
			_numUnits = pNum;
			if (_numUnits >= 300) 
			{
				_numUnits = 300;
			}
			desTex.txt.text = Math.round(_numUnits).toString();
			
			if (_numUnits > _maxUnits) 
			{
				_radius = _numUnits / 3;
				TweenLite.to(_cell,0.2,{scaleX:_radius / 30,scaleY:_radius / 30,ease:Linear.easeNone});
			}
			else if (_numUnits > _maxUnits && _numUnits < _maxUnits && _radius > _maxUnits / 3) 
			{
				_radius = _maxUnits / 3;
				TweenLite.to(_cell,0.5,{scaleX:_radius / 30,scaleY:_radius / 30,ease:Linear.easeNone});
			}
		}
		
		public function get numUnits():Number
		{
			return _numUnits;
		}
		
		/**
		 * 取得随机Vec 
		 * @return 
		 * 
		 */
		public function get randVec():Vec2D
		{
			var loc1:Number=_radius / 2;
			return new Vec2D(x + (-loc1 + Math.random() * _radius), y + (-loc1 + Math.random() * _radius));
		}
		
		public function get vec():Vec2D
		{
			return new Vec2D(x, y);
		}
		
		public function set isStart(pStart:Boolean):void
		{
			_isStart = pStart;
		}
		
		public function get radius():Number
		{
			return _radius;
		}
		
		public function set radius(arg1:Number):void
		{
			_radius = arg1;
			if(desTex)
			{
				desTex.width = _radius;
			}
		}
		
		/**
		 * 设置单元类型 
		 * @param pType
		 * 
		 */
		public function set unitType(pType:int):void
		{
			_unitType = pType;
			var tmpUnit:Object=StaticIni.getIniObj(pType.toString());
			if (!tmpUnit) 
			{
				_cell.alpha = 0.6;//inards_mc
				_cell.alpha = 0.6;//nucleus_mc
				return;
			}
			TweenLite.to(this,0.5,{color:tmpUnit.color,ease:Linear.easeNone});
			//TweenUtil.addTween(_cell, {"_color":tmpUnit.color, "alpha":1, "time":0.5, "transition":Equations.easeNone});
			//Tweener.addTween(_cell.nucleus_mc, {"_color":tmpUnit.color, "alpha":1, "time":0.5, "transition":Equations.easeNone});
			_unitValue = tmpUnit.value;
		}
		
		public function get color():uint
		{
			return _color;
		}
		
		/**
		 * 设置颜色 
		 * @param val
		 * 
		 */
		public function set color(val:uint):void
		{
			_color	=	val;
			var tmpTransform	:ColorTransform=new ColorTransform();
			tmpTransform.color = val;
			_cell.transform.colorTransform = tmpTransform;
			_cell.alpha	=	1;
		}
		
		public function updateMove(pTime:uint, arg2:Number):void
		{
			if (x + _radius > GameStage.rootW) 
			{
				if (ov.x > 0) 
				{
					ov.x = -ov.x;
				}
				vel = (1 - (x + _radius - GameStage.rootW) / _radius) * 25 + 1;
			}
			else if (x - _radius < 0) 
			{
				if (ov.x < 0) 
				{
					ov.x = Math.abs(ov.x);
				}
				vel = (1 - Math.abs(x - _radius) / _radius) * 25 + 1;
			}
			if (y + _radius > GameStage.rootH) 
			{
				if (ov.y > 0) 
				{
					ov.y = -ov.y;
				}
				vel = (1 - (y + _radius - GameStage.rootH) / _radius) * 25 + 1;
			}
			else if (y - _radius < 0) 
			{
				if (ov.y < 0) 
				{
					ov.y = Math.abs(ov.y);
				}
				vel = (1 - Math.abs(y - _radius) / _radius) * 25 + 1;
			}
			if (vel > 1) 
			{
				vel = vel * friction;
			}
			else 
			{
				vel = 0;
			}
			var tmpVec:Vec2D=ov.mulNum(vel);
			x = x + tmpVec.x * arg2;
			y = y + tmpVec.y * arg2;
			if (unitType < 0) 
			{
				return;
			}
			_curTime = pTime - _lastTime;
			_totalTime = (1 - _radius / 100) * 1000 * _unitValue;
			if (_curTime >= _totalTime) 
			{
				_lastTime = pTime;
				if (_numUnits < _maxUnits) 
				{
					numUnits++;
				}
				else if (_numUnits > _maxUnits) 
				{
					numUnits--;
				}
			}
		}
		
		public function get isStart():Boolean
		{
			return _isStart;
		}
		
		public function get unitType():int
		{
			return _unitType;
		}
		
		public function explode():void
		{
			//Tweener.addTween(this, {"alpha":0, "scaleX":0, "scaleY":0, "time":0.3, "transition":Equations.easeInBack});
		}
		
		/**
		 * 是否显示数量 
		 * @param pShow
		 * 
		 */
		public function set showUnits(pShow:Boolean):void
		{
			_showUnits = pShow;
			desTex.visible = _showUnits;
		}
		
		/**
		 * 取得最大数量 
		 * @return 
		 * 
		 */
		public function get maxUnits():uint
		{
			return _maxUnits;
		}
		
		/**
		 * 取得是否显示数量 
		 * @return 
		 * 
		 */
		public function get showUnits():Boolean
		{
			return _showUnits;
		}

		/**
		 * 是否显示外框 
		 */
		public function get ringShow():Boolean
		{
			return _ringShow;
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		/**
		 * 设置选中状态 
		 * @param value
		 * 
		 */
		public function set selected(value:Boolean):void
		{
			_selected = value;
		}
	}
}